package com.edgar.fileupload;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.edgar.baselibrary.utils.SpannedUtils;
import com.edgar.fileupload.library.OnUploadListener;
import com.edgar.fileupload.library.UploadFileManager;
import com.edgar.loglib.LogUtils;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private UploadFileManager uploadFileManager = UploadFileManager.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uploadFileManager.init(getApplicationContext());
        LogUtils.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.text);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//        textView.setText(SpannedUtils.convertSpanned("总分:60分", Color.BLUE, 3, "总分:60分".length()));
        String html = "总分:<font color=red>60分</font>";
        textView.setText(SpannedUtils.convertHtmlSpanned("总分:","60分","red"));
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        LogUtils.i("MainActivity","onCreate",new Throwable());
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                File file = new File(Environment.getExternalStorageDirectory(),"haha.png");
                uploadFileManager.uploadFile("http://napp.qida.com/app/attachment/upload", file,null, new OnUploadListener() {
                    @Override
                    public void onUploadProgress(String fileName, int progress) {
                    }

                    @Override
                    public void onUploadFailed(String fileName) {
                    }

                    @Override
                    public void onUploadFileFinish(String fileName) {
                    }

                    @Override
                    public void onResponse(String result) {
                        System.out.println("响应:" + result);
                    }
                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
